package 力扣._67_二进制求和;

public class Solution {
    public String addBinary(String a, String b) {
        int jinwei = 0;
        StringBuilder s = new StringBuilder();
        int i = a.length() - 1, j = b.length() - 1;
        for (; i >= 0 && j >= 0; i--, j--) {
            int b1 = a.charAt(i) - '0';
            int b2 = b.charAt(j) - '0';
            char c = (char) ('0' + (b1 + b2 + jinwei) % 2);
            s.insert(0, c);
            jinwei = (b1 + b2 + jinwei) / 2;
        }
        while (j >= 0) {
            int b2 = b.charAt(j--) - '0';
            char c = (char) ('0' + (b2 + jinwei) % 2);
            s.insert(0, c);
            jinwei = (b2 + jinwei) / 2;
        }
        while (i >= 0) {
            int b1 = a.charAt(i--) - '0';
            char c = (char) ('0' + (b1 + jinwei) % 2);
            s.insert(0, c);
            jinwei = (b1 + jinwei) / 2;
        }
        if (jinwei == 1)
            s.insert(0, '1');
        return s.toString();

    }
}
